$(function(){
	var queryObj = {
		'sp[tableNames]' : tableNames,
		'sp[columnNames]' : columnNames,
		'sp[queryParam]' : queryParam
	};
	
	dataLogList(queryObj);
});

// 数据修改日志
function dataLogList(queryObj){
	var dataLogList = $("#dataLogList");
	dataLogList.jqGrid({
		caption: '<a href="javascript:void(0)" onclick="toggletListGridState(this);"><i class="fa fa-plus"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;' + caption,
		url: ctx + 'sys/dataLog/dataLogListPage.do',
		datatype: 'json',
		colNames: ['ID', 'logId', '操作人', '操作时间', '修改的表名', 'columnName', "修改的字段", '修改之前的值','修改之后的值', '操作类型', '备注'],
		colModel: [
			      {name:'detail',index:',  hidden: true',  hidden: true, key: true },
			      {name:'logId',index:'logId',  hidden: true},
			      {name:'operationUserNM',index:'operationUserNM'},
			      {name:'operationDate',index:'operationDate'},
			      {name:'tableName',index:'tableName', hidden: true},
			      {name:'columnName',index:'columnName', hidden: true},
			      {name:'columnComment',index:'columnComment'},
			      {name:'oldValue',index:'oldValue', formatter: formatterRouter},
			      {name:'newValue',index:'newValue', formatter: formatterRouter},
			      {name:'operationTypeText',index:'operationTypeText'},
			      {name:'remark',index:'remark', hidden: true}
				  ],
		rowNum:20,        
		rowList:[20,30,50],  
		rownumbers : true,
		postData: queryObj,
		rownumWidth : 50,
		prmNames : {
			search : "search",
			page : "pageNo",
			rows : "limit"
		},
		height : 'auto',
		width : 'auto',
		autowidth : true,
		shrinkToFit : true,
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		jsonReader : {
			root : "items", // 结果集
			records : "total", // 总记录数
			total : "pageCount", // 总页数
			page : "pageNo", // 当前页
			repeatitems : false
		},
		multiselect : false,
		viewrecords : true,
		hiddengrid: true, //
		hidegrid: true, // 隐藏/显示按钮是否显示
		onHeaderClick: function(gridstate) {
			debugger;
			if (gridstate == 'visible') {
				dataLogList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
				dataLogList.jqGrid("setCaption", '<a href="javascript:void(0)" onclick="toggletListGridState(this);"><i class="fa fa-minus"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;' + caption);
			} else {
				dataLogList.jqGrid("setCaption", '<a href="javascript:void(0)" onclick="toggletListGridState(this);"><i class="fa fa-plus"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;' + caption);
			}
		},
		pager: "#dataLogListPage"
	});
	jQuery("#dataLogList").navGrid('#dataLogListPage',{edit:false,add:false,del:false,search:false});
	
	$(".ui-jqgrid-titlebar .HeaderButton").hide();
}

//自动适应宽度
$(window).resize(function(){
	var parentWidth = $("#dataLogListDiv").width();
	$("#dataLogList").jqGrid('setGridWidth', parentWidth );
});

function toggletListGridState(obj, $list) {
	var gridState = $("#dataLogList").jqGrid('getGridParam', 'gridstate');
	gridState = gridState == 'visible' ? 'hidden' : 'visible';
	
	var clazz = gridState == 'visible' ? 'fa fa-minus' : 'fa fa-plus';
	$(obj).children("i").attr("class", clazz);
	
	$("#dataLogList").jqGrid('setGridState', gridState);
	
	$("#dataLogList").jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
}

// 数据格式化路由
function formatterRouter(cData, colModel, rData) {
	var result = "";
	var dataFormatterRouterRule = JSON.parse(columnFormatter);
	for (o in dataFormatterRouterRule) {
		if (rData.columnName == o) {
			if (dataFormatterRouterRule[o] == 'dateFormatter') {
				result = dateFormatter(cData);
			}
			if (dataFormatterRouterRule[o] == 'numberFormatter' && cData != null) {
				result = addFormatNumber(cData);
			}
		}
	}
	return result;
}

// 日期类型格式化，只保留年月日
function dateFormatter(cData) {
	return cData ? cData.replace(" 00:00:00", "") : "";
}